package com.bsb.biz.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.mapper.BsbFirmMapper;
import com.bsb.base.mapper.BsbUserMapper;
import com.bsb.base.model.BsbFirm;
import com.bsb.base.model.BsbFirmExample;
import com.bsb.base.model.BsbUser;
import com.bsb.base.model.BsbUserExample;
import com.bsb.base.model.BsbUserExample.Criteria;
import com.bsb.base.model.SysOrganization;
import com.bsb.base.model.SysOrganizationExample;
import com.bsb.base.model.SysUser;
import com.bsb.base.model.SysUserExample;

@Service
public class BsbFirmService{
	
	@Resource
	private BsbFirmMapper bsbFirmMapper;
	@Resource
	private BsbUserMapper bsbUserMapper;
	@Resource
	private SysOrganizationService sysOrganizationService;
	@Resource
	private SysUserService sysUserService;
	
	public String getPhoneByFirmId(Integer firmId){
		BsbUserExample example = new BsbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andFirmIdEqualTo(firmId);
		criteria.andRoleEqualTo("2");
		List<BsbUser> admins = bsbUserMapper.selectByExample(example );
		if(null != admins && admins.size()==1){
			return admins.get(0).getPhone();
		}
		return null;
	}
	
	public void addFirm(BsbFirm bsbFirm,BsbUser user) {
		bsbFirmMapper.insertSelective(bsbFirm);
		user.setFirmId(bsbFirm.getAccount());
		bsbUserMapper.insertSelective(user);
	}
	
	public void addBsbFirm(BsbFirm bsbFirm) {
		bsbFirmMapper.insertSelective(bsbFirm);
	}
	
	public void delBsbFirmByAccount(Integer account) {
		bsbFirmMapper.deleteByPrimaryKey(account);
	}
	
	public void updateBsbFirm(BsbFirm bsbFirm) {
		bsbFirmMapper.updateByPrimaryKeySelective(bsbFirm);
	}
	
	public void updateBsbFirmByExample(BsbFirm bsbFirm,BsbFirmExample bsbFirmExample){
	    bsbFirmMapper.updateByExampleSelective(bsbFirm,bsbFirmExample);
	}
	
	public BsbFirm getBsbFirmByAccount(Integer account) {
		return bsbFirmMapper.selectByPrimaryKey(account);
	}
	
	public List<BsbFirm> getPageBsbFirm(BsbFirmExample bsbFirmExample) {
		return bsbFirmMapper.selectPage(bsbFirmExample);
	}
	
	public int countBsbFirm(BsbFirmExample bsbFirmExample) {
		return bsbFirmMapper.countByExample(bsbFirmExample);
	}
	
	public List<BsbFirm> selectByExample(BsbFirmExample example){
	    return bsbFirmMapper.selectByExample(example);
	}

	public void addFirmAndDept(BsbFirm bsbFirm, SysOrganization org) {
		bsbFirmMapper.insertSelective(bsbFirm);
		org.setFirmId(bsbFirm.getAccount().longValue());
		sysOrganizationService.addSysOrganization(org);
	}
	/**
	 * 删除事务所  1事务所状态改为已删除，后台事务所列表不显示
			  2事务所管理员状态改为已删除禁止登陆后台
			  3事务所下的所有咨询师普通用户所属机构改为平台
			  4部门表状态修改
	 * @param account
	 */
	public void delBsbFirm(Long account) {
		//事务所状态删除
		BsbFirm record = new BsbFirm();
		record.setAccount(account.intValue());
		record.setDelFlag(Global.HIDE);
		bsbFirmMapper.updateByPrimaryKeySelective(record);
		//咨询师普通用户改为平台普通用户
		BsbUserExample exam = new BsbUserExample();
		Criteria createria = exam.createCriteria();
		createria.andFirmIdEqualTo(account.intValue());
		BsbUser bsbuser = new BsbUser();
		bsbuser.setFirmId(010000);
		bsbuser.setRole("1");
		bsbUserMapper.updateByExampleSelective(bsbuser , exam);
		//部门状态改为禁用
		SysOrganization sysOrganization = new SysOrganization();
		sysOrganization.setAvailable(false);
		SysOrganizationExample sysOrganizationExample = new SysOrganizationExample();
		com.bsb.base.model.SysOrganizationExample.Criteria crriteria = sysOrganizationExample.createCriteria();
		crriteria.andFirmIdEqualTo(account);
		sysOrganizationService.updateSysOrganizationByExample(sysOrganization, sysOrganizationExample);
		//管理员 状态改为禁用
		SysOrganizationExample example = new SysOrganizationExample();
		com.bsb.base.model.SysOrganizationExample.Criteria creriteria = example.createCriteria();
		creriteria.andFirmIdEqualTo(account);
		List<SysOrganization> dept = sysOrganizationService.selectByExample(example );
		if(null == dept || dept.size()==0){
			return;
		}
		Long deptid = dept.get(0).getId();
		SysUser user = new SysUser();
		user.setLocked(true);
		SysUserExample sysUserExample = new SysUserExample();
		com.bsb.base.model.SysUserExample.Criteria criteria = sysUserExample.createCriteria();
		criteria.andOrganization_idEqualTo(deptid);
		sysUserService.updateSysUserByExample(user, sysUserExample );
		
		
	}
	
	public List<Integer> getIdsLikeName(String name){
		if(StringUtils.isEmpty(name)){
			return Collections.EMPTY_LIST;
		}
		List<Integer> list = new ArrayList<Integer>();
		BsbFirmExample example = new BsbFirmExample();
		com.bsb.base.model.BsbFirmExample.Criteria criteria = example.createCriteria();
		criteria.andNameLike("%"+name+"%");
		List<BsbFirm> firms = bsbFirmMapper.selectByExample(example );
		for (BsbFirm bsbFirm : firms) {
			Integer account = bsbFirm.getAccount();
			list.add(account);
		}
		return list;
	}
}
